
var btn_txt = "验证";
var has_verify_code = 0;
window.onload = function () {
  document.getElementsByTagName('body')[0].style.height = document.documentElement.clientHeight + "px";
  // var token = document.getElementsByTagName('input')[2].value;
  // if(token){
  //   localStorage.setItem('token',token);
  // }
  var url = document.getElementsByTagName('input')[4].value;
  new QRCode(document.getElementById("qrcode"), {
    text: url,
    width: 300,
    height: 300,
    colorDark: "#000000",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.L
  });
  
  has_verify_code = document.getElementsByTagName('input')[5].value;
  var is_verify = document.getElementsByTagName('input')[6].value;
  var book_type_txt = document.getElementsByTagName('input')[7].value;
  if(has_verify_code == 2){
    btn_txt = "兑换";
  }
  
  if(is_verify == 1) {
    var data = {};
    data.title = "提示信息";    
      data.line1 = "您已经" + btn_txt + "过该" + book_type_txt;
      data.line2 = "请进入公众号查看内容";
      data.titleColorType = "1";
      data.times = 1;      
      showModal(data);
  document.getElementById('happybuy').style.display = "";
      
  }
};
function showQrCode() {
  document.getElementById('modal').style.display = "none";
  document.getElementsByClassName('book')[0].style.display = "none";
  document.getElementsByClassName('qrcode')[0].style.display = "";
  var bottom = document.getElementsByClassName('bottom-bar');
  bottom[0].style.display = "none";
  bottom[1].style.display = "";
}
function reVerify() {
  document.getElementsByTagName('input')[0].value = "";
  document.getElementById('modal').style.display = "none";
}
function setVerifyBtnDisabled(disabled) {
  var btn = document.getElementById('verify-btn');
  if (disabled) {
    btn.innerText = btn_txt + "中...";
    btn.style.background = "#f7cc86";
    btn.style.fontSize = "0.28rem";
    btn.disabled = true;
  } else {
    btn.innerText = btn_txt;
    btn.style.background = "#F5A623";
    btn.style.fontSize = "0.36rem";
    btn.disabled = false;
  }
}
function showModal(data) {
  var modal = document.getElementById('modal');
  var title = modal.getElementsByClassName("title")[0];
  title.innerText = data.title;
  if (data.titleColorType == 1) {
    title.style.color = "#00A874";
  } else {
    document.getElementsByClassName("qrcode-info")[0].style.display = "none";
    document.getElementsByClassName("qrcode-info")[1].style.display = "";
    title.style.color = "#D0021B";
  }
  modal.getElementsByClassName("detail")[0].innerText = data.line1;
  modal.getElementsByClassName("detail")[1].innerText = data.line2;
  if (data.times == 0) {
    modal.getElementsByClassName("ghost")[0].style.display = "";
    modal.getElementsByClassName("primary")[0].innerText = "立即反馈";
  } else {
    modal.getElementsByClassName("ghost")[0].style.display = "none";
    modal.getElementsByClassName("primary")[0].innerText = "知道了";
  }
  modal.style.display = "";
}
function textObj(textStr) {
  var text = document.querySelector('.occlusion').querySelector('.text');
  var occlusion = document.querySelector('.occlusion');
  occlusion.style.display = "block";
  text.innerHTML = textStr;
  occlusion.getElementsByTagName('button')[0].onclick = function () {
    document.querySelector('.occlusion').style.display = "none";
  };
}
function verify() {
  var inputData = document.getElementsByTagName('input');
  var code = inputData[0].value.trim();
  if (code.length == 0) {
    if(has_verify_code == 2){
      textObj('请输入兑换码!');
    }else{
      textObj('请输入防伪码!');
    }
    return;
  }
  if (code.length != 10) {
    if(has_verify_code == 2){
      textObj('请输入正确的兑换码!');
    }else{
      textObj('请输入正确的防伪码!');
    }
    return;
  }
  setVerifyBtnDisabled(true);
  var api = axios.create({
    baseURL: 'https://api.tl100.com/bookex',
    withCredentials: true,
    timeout: 5000
  });
  var params = {};
  params.code = code.toUpperCase();
  params.book_id = inputData[1].value;
  params.token = inputData[2].value;
  params.ip = inputData[3].value;
  //正版验证
  if(has_verify_code == 1) {
    api.post('api/v1.0/book_verify/bookVerifyByScanQR', params)
    .then(function (response) {
      setVerifyBtnDisabled(false);
      var rs_data = response.data;
      var data = {};
      if (rs_data.res_code == 1) {
        data.times = rs_data.msg.hits;
        if (data.times == 1) {
          data.title = "该图书为正版";
          data.titleColorType = "1";
          data.line1 = "防伪码首次验证成功";
          data.line2 = "50点消费点已充至您的账号";
        } else if (data.times <= 10) {
          data.title = "该图书为正版";
          data.titleColorType = "1";
          data.line1 = "已验证" + data.times + "次";
          data.line2 = "上次验证时间：" + moment(rs_data.msg.last_use_time).format("YYYY-MM-DD HH:mm:ss");
        } else {
          data.title = "该图书疑似盗版";
          data.titleColorType = "2";
          data.line1 = "该防伪码已验证" + data.times + "次";
          data.line2 = "请确认是否本人操作";
        }
      document.getElementById('happybuy').style.display = "";
      } else {
        //验证失败
        data.times = 0;
        data.title = "图书验证失败";
        data.titleColorType = "2";
        data.line1 = "很遗憾，您的图书疑似盗版";
        data.line2 = "请检查图书防伪码是否正确";
      }
      console.log(data);
      showModal(data);
    })
    .catch(function (error) {
      alert("验证超时！");
      setVerifyBtnDisabled(false);
    });
  }
  //图书兑换
  if(has_verify_code == 2){
    api.post('api/v1.0/book_verify/getExByCode', params)
    .then(function (response) {
      setVerifyBtnDisabled(false);
      var rs_data = response.data;
      var data = {};
      data.line1 = rs_data.msg;
      data.line2 = "";
      if (rs_data.res_code == 1) {
          data.title = "该图书为正版";
          data.titleColorType = "1";
          data.times = 1; 
      document.getElementById('happybuy').style.display = "";
      } else {
        //验证失败
        data.title = "图书验证失败";
        data.titleColorType = "2";
        data.times = 0; 
      }
      console.log(data);
      showModal(data);
    })
    .catch(function (error) {
      textObj('验证超时!');
      setVerifyBtnDisabled(false);
    });
  }
}
function goHappyBuy(eid,sid){
  location.href = "tlmall/intro?exam_id=" + eid + "&sale_id=" + sid+ "&o=qrcode";
}